<template>
  <div class="container">
    <el-card>
      <div slot="header">
        <span style="font-weight: 700; font-size: 22px">文章列表</span>
        <el-button
          style="float: right"
          type="primary"
          size="small"
          @click="addCategory"
          >添加分类</el-button
        >
      </div>
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column
          prop="id"
          label="序号"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="cate_name"
          label="分类名称"
          width="620"
        ></el-table-column>
        <el-table-column
          prop="cate_alias"
          label="分类别名"
          width="620"
        ></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="{row}">
            <el-button type="primary" size="small" @click="edit(row.id)">修改</el-button>
            <el-button type="danger" size="small" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="title" :visible="showDialog" @close="btnCancel" width="25%">
      <el-form ref="form" :model="form" :rules="rules" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="form.cate_name" style="width: 95%"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="form.cate_alias" style="width: 95%"></el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="7">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addArticleCategory, delArticleCategory, editArticleCategory, getArticleCategory, getCategoryInfo } from '@/api/article'
export default {
  name: 'article-category',
  data () {
    return {
      showDialog: false,
      tableData: [],
      form: {
        cate_name: '',
        cate_alias: ''
      },
      title: '添加分类',
      categoryInfo: {},
      rules: {
        cate_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名不能为空', trigger: 'blur' },
          { pattern: /^(?=.*?[a-z])(?=.*?[0-9])(?=.*?[_\-@&=])[a-z0-9_\-@&=]+$/, message: '格式不正确，别名由字母数字字符组成', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getArticleCategory()
  },
  methods: {
    async getArticleCategory () {
      const res = await getArticleCategory()
      this.tableData = res.data
      console.log(res)
    },
    addCategory () {
      this.showDialog = true
      this.title = '添加分类'
    },
    async edit (id) {
      this.showDialog = true
      this.title = '编辑分类'
      const res = await getCategoryInfo(id)
      this.form = res.data
      console.log(res)
    },
    async del (id) {
      console.log(id)
      await this.$confirm('确定删除嘛')
      await delArticleCategory(id)
      this.$message.success('删除成功')
      this.getArticleCategory()
    },
    async btnOK () {
      if (this.form.id) {
        // 修改分类
        await editArticleCategory(this.form)
        this.$message.success('分类修改成功')
      } else {
        // 新增分类
        await addArticleCategory(this.form)
      }
      this.getArticleCategory()
      this.showDialog = false
    },
    async btnCancel () {
      this.form = {
        cate_name: '',
        cate_alias: ''
      }
      await this.$refs.form.resetFields()
      this.showDialog = false
    }
  }
}
</script>

<style>
</style>
